{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "26e8b0ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----!"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'word': 'sarah',\n",
       "  'score': 0.8125942349433899,\n",
       "  'entity': 'B-name',\n",
       "  'index': 4,\n",
       "  'start': 11,\n",
       "  'end': 16},\n",
       " {'word': 'jessica',\n",
       "  'score': 0.8755828142166138,\n",
       "  'entity': 'I-name',\n",
       "  'index': 5,\n",
       "  'start': 17,\n",
       "  'end': 24},\n",
       " {'word': 'parker',\n",
       "  'score': 0.9242430329322815,\n",
       "  'entity': 'I-name',\n",
       "  'index': 6,\n",
       "  'start': 25,\n",
       "  'end': 31}]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sagemaker.huggingface import HuggingFaceModel\n",
    "import sagemaker\n",
    "\n",
    "role = sagemaker.get_execution_role()\n",
    "# Hub Model configuration. https://huggingface.co/models\n",
    "hub = {\n",
    "\t'HF_MODEL_ID':'uer/roberta-base-finetuned-cluener2020-chinese',\n",
    "\t'HF_TASK':'token-classification'\n",
    "}\n",
    "\n",
    "# create Hugging Face Model Class\n",
    "huggingface_model = HuggingFaceModel(\n",
    "\ttransformers_version='4.6.1',\n",
    "\tpytorch_version='1.7.1',\n",
    "\tpy_version='py36',\n",
    "\tenv=hub,\n",
    "\trole=role, \n",
    ")\n",
    "\n",
    "# deploy model to SageMaker Inference\n",
    "predictor = huggingface_model.deploy(\n",
    "\tinitial_instance_count=1, # number of instances\n",
    "\tinstance_type='ml.m5.xlarge' # ec2 instance type\n",
    ")\n",
    "\n",
    "predictor.predict({\n",
    "\t'inputs': \"My name is Sarah Jessica Parker but you can call me Jessica\"\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "511e166f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'word': '江',\n",
       "  'score': 0.6618994474411011,\n",
       "  'entity': 'B-address',\n",
       "  'index': 1,\n",
       "  'start': 0,\n",
       "  'end': 1},\n",
       " {'word': '苏',\n",
       "  'score': 0.5544544458389282,\n",
       "  'entity': 'I-address',\n",
       "  'index': 2,\n",
       "  'start': 1,\n",
       "  'end': 2},\n",
       " {'word': '特',\n",
       "  'score': 0.4227263331413269,\n",
       "  'entity': 'B-company',\n",
       "  'index': 7,\n",
       "  'start': 6,\n",
       "  'end': 7},\n",
       " {'word': '斯',\n",
       "  'score': 0.45469754934310913,\n",
       "  'entity': 'I-company',\n",
       "  'index': 8,\n",
       "  'start': 7,\n",
       "  'end': 8},\n",
       " {'word': '拉',\n",
       "  'score': 0.5207826495170593,\n",
       "  'entity': 'I-company',\n",
       "  'index': 9,\n",
       "  'start': 8,\n",
       "  'end': 9}]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictor.predict({\n",
    "\t'inputs': \"江苏警方通报特斯拉冲进店铺\"\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "36b1df6e",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'HuggingFacePredictor' object has no attribute 'delete'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-6-a7fba4e12545>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mpredictor\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdelete\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m: 'HuggingFacePredictor' object has no attribute 'delete'"
     ]
    }
   ],
   "source": [
    "predictor.delete()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "de8acb93",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "conda_python3",
   "language": "python",
   "name": "conda_python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
